package com.woniu.intlet.web;


import com.woniu.command.purchasedetailcommand.AddPurchaseDetailCommand;
import com.woniu.command.purchasedetailcommand.DeletePurchaseDetailCommand;
import com.woniu.command.purchasedetailcommand.UpdatePurchaseDetailCommand;
import com.woniu.inlet.vo.MaterialBomVo;
import com.woniu.outlet.po.BomPo;
import com.woniu.outlet.po.PurchaseDetailPo;
import com.woniu.service.BomPoService;
import com.woniu.service.PurchaseDetailPoService;
import com.woniu.uitl.ResponseResult;
import com.woniu.uitl.ResultCode;
import com.woniu.uitl.SnowflakeIdGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


@RestController
@Api(tags = "采购详情接口文档")
public class PurchaseDetailController {

    @Autowired
    private PurchaseDetailPoService detailPoService;
    @Autowired
    private BomPoService bomPoService;

    @GetMapping("purchaseDetailList")
    @ApiOperation("采购详情列表")
    public ResponseResult<List<PurchaseDetailPo>> findAllPurchaseDetails(Long id){
        List<PurchaseDetailPo> poList = detailPoService.findAll(id);
        return new ResponseResult<List<PurchaseDetailPo>>(poList,"OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("getBom")
    @ApiOperation("获取物料信息")
    public ResponseResult<MaterialBomVo> getBomByCode(String materialCode){
        MaterialBomVo materialBomVo = bomPoService.getBomByCode(materialCode);
        if (StringUtils.isEmpty(materialBomVo)){
            MaterialBomVo bomPo = new MaterialBomVo();
            bomPo.setMaterialName("查询物料不存在");
            return new ResponseResult<MaterialBomVo>(materialBomVo,"OK",ResultCode.SUCCESS.getCode());
        }
        return new ResponseResult<MaterialBomVo>(materialBomVo,"OK",ResultCode.SUCCESS.getCode());
    }

    /**
     *
     * @param
     * @param id  采购单id
     * @param num 采购数量
     * @return
     */
    @PostMapping("addPurchaseDetail")
    @ApiOperation("添加采购详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materCode",required = true,value = "物料编码"),
            @ApiImplicitParam(name = "id",required = true,value = "采购单id"),
            @ApiImplicitParam(name = "num",required = true,value = "采购数量")
    })
    public ResponseResult<String> addPurchaseDetail(String materCode, Long id, Integer num){
        MaterialBomVo bomByCode = bomPoService.getBomByCode(materCode);
        //判断是否重复添加
        List<PurchaseDetailPo> purByCode = detailPoService.getPurByCode(materCode);
        if (purByCode.size() == 0){
            SnowflakeIdGenerator idWorkes = new SnowflakeIdGenerator(1, 1);
            long id1 = idWorkes.nextId();
            //补全信息
            PurchaseDetailPo detailPo = new PurchaseDetailPo();
            detailPo.setId(id1);
            detailPo.setMaterialCode(materCode);
            detailPo.setMaterialName(bomByCode.getMaterialName());
            detailPo.setBomId(bomByCode.getId());
            detailPo.setPurchaseId(id);
            detailPo.setPurchaseNum(num);
            detailPo.setPurchaseTaxPrice(new BigDecimal(bomByCode.getPurchaseTaxPrice()));
            detailPo.setTheoreticalWeight(bomByCode.getTheoreticalWeight());
            detailPo.setSpecificationUnit(bomByCode.getSpecificationUnit());
            detailPo.setColor(bomByCode.getColor());
            //计算金额
            Double money = num * bomByCode.getPurchaseTaxPrice();
            detailPo.setTotalMoney(new BigDecimal(money));

            AddPurchaseDetailCommand addCommand = new AddPurchaseDetailCommand();
            String msg = addCommand.excute(detailPo);
            return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
        }else {
            //已经存在,直接加数量改金额
            PurchaseDetailPo detailPo2 = purByCode.get(0);
            //计算金额
            Double money = num * bomByCode.getPurchaseTaxPrice();

            detailPo2.setPurchaseNum(detailPo2.getPurchaseNum()+num);
            detailPo2.setTotalMoney(detailPo2.getTotalMoney().add(new BigDecimal(money)));
            UpdatePurchaseDetailCommand command = new UpdatePurchaseDetailCommand();
            String msg = command.excute(detailPo2);
            return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),"添加成功");
        }
    }

    @PutMapping("updatePurchaseDetail")
    @ApiOperation("修改采购详细信息")
    public ResponseResult<String> updatePur(PurchaseDetailPo detailPo){
        UpdatePurchaseDetailCommand command = new UpdatePurchaseDetailCommand();
        String msg = command.excute(detailPo);
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
    }

    @DeleteMapping("deletePurchaseDetail")
    @ApiOperation("删除采购详情")
    public ResponseResult<String> delete(Long id){
        DeletePurchaseDetailCommand command = new DeletePurchaseDetailCommand();
        String msg = command.excute(id);
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
    }

}
